{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Group Words that are Anagrams\n",
    "\n",
    "[原题](https://mp.weixin.qq.com/s/cpLfIsygSw9pgZtxBQLS_g)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "Given a list of words, group the words that are *anagrams* of each other.\n",
    "\n",
    "**Note:** An *anagram* are words made up of the same letters."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "```text\n",
    "Input: ['abc', 'bcd', 'cba', 'cbd', 'efg']\n",
    "Output: [['abc', 'cba'], ['bcd', 'cbd'], ['efg']]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "''' Global Imports '''\n",
    "import collections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def groupAnagramWords(strs):\n",
    "    ''' Main Algorithm '''\n",
    "    \n",
    "    # Count all letters in strs.\n",
    "    counters = [collections.Counter(string) for string in strs]\n",
    "    \n",
    "    # Create a result set.\n",
    "    result = []\n",
    "    \n",
    "    for i in range(len(counters)):\n",
    "        # Make sure it hasn't been checked before.\n",
    "        if strs[i] in [elem for row in result for elem in row]:\n",
    "            continue\n",
    "        \n",
    "        # Append it to a temparory list.\n",
    "        temp = [strs[i]]\n",
    "        \n",
    "        for j in range(i + 1, len(counters)):\n",
    "            # They have the same letters and\n",
    "            # one of them hasn't been checked,\n",
    "            if (counters[i] == counters[j] and\n",
    "                strs[j] not in [\n",
    "                    elem for row in result\n",
    "                    for elem in row\n",
    "                ]):\n",
    "                # Append it to the current list.\n",
    "                temp.append(strs[j])\n",
    "        \n",
    "        # Append it to the result list.\n",
    "        result.append(temp)\n",
    "        \n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['abc', 'cba'], ['bcd', 'cbd'], ['efg']]\n"
     ]
    }
   ],
   "source": [
    "print(groupAnagramWords(['abc', 'bcd', 'cba', 'cbd', 'efg']))\n",
    "# [['efg'], ['bcd', 'cbd'], ['abc', 'cba']]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
